package com.sdkj.service.materials.getmaterials;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdkj.activiti.util.ActivitiUtils;
import com.sdkj.dao.DaoSupport;
import com.sdkj.entity.Page;
import com.sdkj.service.activiti.ActTaskService;
import com.sdkj.util.PageData;
import com.sdkj.util.UuidUtil;


@Service("applygetmaterialsService")
public class ApplyGetMaterialsService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;
	@Autowired
	private IdentityService identityService;
	
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	ApplyGetMaterialsDetailService applygetmaterialsdetailService;
	
	
	/*
	* 新增
	*/
	public void save(PageData pd)throws Exception{
		
		String primaryKey=pd.getString("apply_get_materials_id");
		String str=pd.getString("apply_kind_num");//已选择的物品
		String idsStr=pd.getString("goods_id");//已选择的物品id
		String[] strs=str.split(",");
		String[] ids=idsStr.split(",");
		//打印纸-单价20.5-数量2-共41元
		for(int i=0;i<strs.length;i++){
			String[] params=strs[i].split("-");
			String name =params[0];
			String price =params[1].substring(params[1].lastIndexOf("价")+1);
			String num =params[2].substring(params[2].lastIndexOf("量")+1);
			String account =params[3].substring(params[3].lastIndexOf("共")+1,params[3].lastIndexOf("元"));
			PageData pdDetail = new PageData();
			pdDetail.put("apply_get_materials_detail_id", UuidUtil.get32UUID());
			pdDetail.put("apply_get_materials_id",primaryKey);
			pdDetail.put("goods_id", ids[i]);
			pdDetail.put("goods_name",name );
			pdDetail.put("goods_price", price);
			pdDetail.put("goods_num", num);
			pdDetail.put("goods_account", account);
			pdDetail.put("remark", "");
			dao.save("ApplyGetMaterialsDetailMapper.save", pdDetail);
		}
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("checkUserId", pd.getString("user_id"));
		String user_id = pd.getString("user_id");
		identityService.setAuthenticatedUserId(user_id);
		String process_instance_id = actTaskService.startProcess(user_id,ActivitiUtils.PD_APPLY_MATERIALS[0],ActivitiUtils.PD_APPLY_MATERIALS[1],primaryKey,pd.getString("remark"),vars);
		pd.put("process_instance_id", process_instance_id);
		dao.save("ApplyGetMaterialsMapper.save", pd);
		
		
		
	}
	
	/*
	* 删除
	*/
	public void delete(PageData pd)throws Exception{
		dao.delete("ApplyGetMaterialsMapper.delete", pd);
	}
	
	/*
	* 修改状态
	*/
	public void updateState(PageData pd)throws Exception{
		dao.delete("ApplyGetMaterialsMapper.edit", pd);
	}
	
	/**
	 * 查询一条信息
	 */
	public HashMap<String,Object> findObjById(PageData pd) throws Exception{

		HashMap<String,Object> map=new HashMap<String, Object>();
		PageData pdApply = (PageData)dao.findForObject("ApplyGetMaterialsMapper.findByPDId", pd);; // 根据ID读取
		Page page = new Page();
		page.setPd(pdApply);
		List<PageData> detailList = applygetmaterialsdetailService.list(page);
		map.put("detailList", detailList);
		map.put("pdApply", pdApply);
		return map;
	}	
	/*
	* 修改审核状态
	*/
	public void updateStateById(PageData pd)throws Exception{
		System.out.println("XXXXXXXXXXX==111");
		dao.delete("ApplyGetMaterialsMapper.edit", pd);
		System.out.println("XXXXXXXXXXX===222");
		String taskId = pd.getString("task_id");
		String procInsId = pd.getString("procIns_id");
		String comment = pd.getString("comment");
		//为申请时间添加系统时间
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("pass", "yes".equals(pd.getString("flag"))? "1" : "0");
		vars.put("userId", pd.getString("user_id"));
		vars.put("checkUserId2", pd.getString("user_id"));
		actTaskService.complete(taskId, procInsId,comment, vars);
		System.out.println("XXXXXXXXXXX==3333");
	}
	
	
	
	
	
	
	/*
	* 修改
	*/
	public void edit(PageData pd)throws Exception{
		dao.update("ApplyGetMaterialsMapper.edit", pd);
		
		String delsStr = pd.getString("dels");// 要删除的物品
		String saveStr = pd.getString("apply_kind_num");// 要增加的物品
		String idsStr = pd.getString("goods_id");// 要增加的id
		String[] dels = delsStr.split(",");
		for (int i = 0; i < dels.length; i++) {
			PageData pdDel = new PageData();
			pdDel.put("apply_get_materials_detail_id", dels[i]);
			dao.delete("ApplyGetMaterialsDetailMapper.delete", pdDel);
		}
		if (saveStr != null && !"".equals(saveStr)) {
			String[] strs = saveStr.split(",");
			String[] ids = idsStr.split(",");
			// 打印纸-单价20.5-数量2-共41元
			for (int i = 0; i < strs.length; i++) {
				String[] params = strs[i].split("-");
				String name = params[0];
				String price = params[1]
						.substring(params[1].lastIndexOf("价") + 1);
				String num = params[2]
						.substring(params[2].lastIndexOf("量") + 1);
				String account = params[3].substring(
						params[3].lastIndexOf("共") + 1,
						params[3].lastIndexOf("元"));
				PageData pdDetail = new PageData();
				pdDetail.put("apply_get_materials_detail_id", UuidUtil.get32UUID());
				pdDetail.put("apply_get_materials_id", pd.getString("apply_get_materials_id"));
				pdDetail.put("goods_id", ids[i]);
				pdDetail.put("goods_name", name);
				pdDetail.put("goods_price", price);
				pdDetail.put("goods_num", num);
				pdDetail.put("goods_account", account);
				pdDetail.put("apply_remark", "");
				dao.save("ApplyGetMaterialsDetailMapper.save", pdDetail);
			}
		}
		
	}
	
	/*
	*列表
	*/
	public List<PageData> list(Page page)throws Exception{
		return (List<PageData>)dao.findForList("ApplyGetMaterialsMapper.datalistPage", page);
	}
	/*
	*财务：物资收费确认
	*/
	public List<PageData> selectmaterialsfeesconfirm(Page page)throws Exception{
		return (List<PageData>)dao.findForList("ApplyGetMaterialsMapper.selectmaterialsfeesconfirm", page);
	}
	
	/*
	*列表(全部)
	*/
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("ApplyGetMaterialsMapper.listAll", pd);
	}
	
	/*
	* 通过id获取数据
	*/
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("ApplyGetMaterialsMapper.findById", pd);
	}
	
	/*
	* 批量删除
	*/
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("ApplyGetMaterialsMapper.deleteAll", ArrayDATA_IDS);
	}
	
}

